package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2020/5/13.
 */
public class UserContext {
    public static final String EMPLOYEE_IN_SESSION = "EMPLOYEE_IN_SESSION";
    public static final String PERMISSION_IN_SESSION = "PERMISSION_IN_SESSION";

    public static HttpSession getSession(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return requestAttributes.getRequest().getSession();
    }

    //获取当前登录用户
    public static Employee getCurrentUser() {
        return  (Employee) getSession().getAttribute(EMPLOYEE_IN_SESSION);
    }
    //设置到session中
    public static void setCurrentUser(Employee employee) {
        getSession().setAttribute(EMPLOYEE_IN_SESSION,employee);
    }
    //获取当前用户权限
    public static List<String> getPermissionExpression() {
        return (List<String>) getSession().getAttribute(PERMISSION_IN_SESSION);
    }
    //设置当前用户的权限到session中
    public static void setPermissionExpression(List<String> permissions) {
        getSession().setAttribute(PERMISSION_IN_SESSION,permissions);
    }
}
